var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    userId:0,
    companyId:0,
    gender:['未知','男','女'],
    userInfo:{},
    sealList:[],
    functionList:[{
      type:'seal_mgr',
      code:1001,
      description:'查看印章列表',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1002,
      description:'添加印章',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1007,
      description:'编辑印章',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1008,
      description:'移动印章到部门',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1009,
      description:'启用或停用印章',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1003,
      description:'删除印章',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1004,
      description:'置换印章',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1005,
      description:'设置电子围栏',
      enableFlag:false
    },{
      type:'seal_mgr',
      code:1006,
      description:'查看报警记录',
      enableFlag:false
    },{
      type:'approval_mgr',
      code:2001,
      description:'设置审批流程',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3008,
      description:'分配管理员',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3001,
      description:'邀请人员',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3002,
      description:'移动人员到部门',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3007,
      description:'查看人员',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3009,
      description:'修改人员备注',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3003,
      description:'解除人员绑定',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3004,
      description:'添加部门',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3005,
      description:'编辑部门',
      enableFlag:false
    },{
      type:'company_mgr',
      code:3006,
      description:'解散部门',
      enableFlag:false
    },{
      type:'statistics_mgr',
      code:4001,
      description:'驾驶舱',
      enableFlag:false
    }],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '管理员信息', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad(options) {
    var _this = this;
    _this.setData({
      userId:parseInt(options.userId),
      companyId:parseInt(options.companyId)
    });
    requestClient = new ZYRequestClient();
  },
  onShow(){
    var _this = this;
    _this.loadUserInfo();
    _this.loadFunctionList();
    _this.loadSealList();
  },
  // 加载用户信息
  loadUserInfo(){
    var _this = this;
    requestClient.getUserInfoByCompany(_this.data.userId,_this.data.companyId,function(res){
      _this.setData({
        userInfo:res.data.data
      });
    });
  },
  // 加载功能权限
  loadFunctionList(){
    var _this = this;
    requestClient.getAdminFunctionList(_this.data.userId,_this.data.companyId,function(res){
      var adminFunctionList = res.data.data;
      var functionList = _this.data.functionList;
      for(var i = 0; i < functionList.length; i++){
        var fun = functionList[i];
        for(var j = 0; j < adminFunctionList.length; j++){
          if(fun.code == adminFunctionList[j].code){
            fun.enableFlag = adminFunctionList[j].enableFlag;
            break;
          }
        }
      }
      _this.setData({
        functionList:functionList
      });
    });
  },
  // 加载印章信息
  loadSealList(){
    var _this = this;
    requestClient.getSealList(_this.data.companyId,function(res){
      var sealList=[];
      requestClient.getAdminSealList(_this.data.userId,_this.data.companyId,function(resFunction){
        var adminSealList = resFunction.data.data;
        for(var i=0;i<res.data.data.length;i++){
          var seal=res.data.data[i];
          var adminSeal = {
            id:seal.id,
            name:seal.name,
            enableFlag:false
          }
          // 循环查找是否匹配
          for(var j = 0;j<adminSealList.length;j++){
            if(adminSeal.id == adminSealList[j].sealId){
              adminSeal.enableFlag = adminSealList[j].enableFlag;
              break;
            }
          }
          sealList.push(adminSeal);
        }
        _this.setData({
          sealList:sealList
        });
      });
    })
  },
  // 选择功能
  bindFunctionChange(e){
    var _this = this;
    var code = e.currentTarget.dataset.code;
    var enableFlag = e.detail.value;
    var fun = {
      code:code,
      userId:_this.data.userId,
      companyId:_this.data.companyId,
      enableFlag:enableFlag
    };
    requestClient.updateAdminFunction(fun,function(res){});
  },
  // 选择印章
  bindSealChange(e){
    var _this = this;
    var id = e.currentTarget.dataset.id;
    var enableFlag = e.detail.value;
    var seal = {
      sealId:id,
      userId:_this.data.userId,
      companyId:_this.data.companyId,
      enableFlag:enableFlag
    };
    requestClient.updateAdminSeal(seal,function(res){});
  }
})
